<?php
/**
 * AintroductionController
 * 
 * @author
 * @version 
 */
require_once 'Zend/Controller/Action.php';
class AintroductionController extends Zend_Controller_Action
{
    protected $_introductionModel;
	
	public function init() 
	{
	     $authService = new Eleven_Service_Authentication();
	           
	    if ($authService->getIdentity()->role != 'Teacher') {
	        $this->_helper->redirector('index','index');
	    }
	    
		$this->_introductionModel = new Eleven_Model_Introduction();
		
		$this->view->editForm = $this->getEditForm();
		
		$this->view->controller = $this->_request->controller;
	}
    /**
     * The default action - show the home page
     */
    public function indexAction ()
    {    
        
    }
    
    public function getEditForm()
	{
		$urlHelper = $this->_helper->getHelper('url');

        $this->_forms['edit'] = $this->_introductionModel->getForm('introductionEdit');
        $this->_forms['edit']->setAction($urlHelper->url(array(
            'controller'	=> 'aintroduction',
            'action' => 'save'
            ),
            'admin'
        ));
        
        $this->_forms['edit']->setMethod('post');
        
        $introduction = $this->_introductionModel->getIntroduction();
        
        $this->_forms['edit']->getElement('title')->setValue($introduction->title);
        $this->_forms['edit']->getElement('content')->setValue((stripslashes($introduction->content)));
       
        return $this->_forms['edit'];
	}
	
    public function saveAction()
	{
		$request = $this->getRequest();
		if (!$request->isPost()) 
		{
			return $this->_helper->redirector('index');
  		}
  		
  		if (false === $this->_introductionModel->saveEdit($request->getPost())) 
  		{
     		return $this->render('index');
  		}
  		
  		return $this->_helper->redirector('index');
	}
}

